package com.kk.score.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.kk.score.dao.TaskDao;
import com.kk.score.entity.ConfigEntity;
import com.kk.score.entity.TaskEntity;
import com.kk.score.service.ConfigService;
import com.kk.score.utils.PageUtils;
import com.kk.score.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.kk.score.entity.PushEntity;
import com.kk.score.service.PushService;

import javax.servlet.http.HttpSession;


/**
 * @author KK
 * @email 409592048@qq.com
 * @date 2022-11-02 12:28:00
 */
@RestController
@RequestMapping("/push")
public class PushController {
    @Autowired
    private PushService pushService;

    @Autowired
    private TaskDao taskDao;

    @Autowired
    private ConfigService configService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = pushService.queryPage(params);

        return R.ok().put("data", page);
    }

    /**
     * 保存
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/save")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R save(@RequestBody PushEntity push, HttpSession session) {
        String departmentUid = push.getDepartmentUid();
        String year = push.getYear();
        PushEntity one = pushService.getOne(new LambdaQueryWrapper<PushEntity>().eq(PushEntity::getYear, year).eq(PushEntity::getDepartmentUid, departmentUid));
        if (!Objects.isNull(one)) {
            return R.error("该单位今年报告任务已生成");
        }
        //获取报告uid
        String reportUid = (String) session.getAttribute(push.getFileName());
        push.setReportUid(reportUid);
        push.setAverage(0.0);
        pushService.save(push);
        //给所有可用的部门下的用户创建任务
        int count = taskDao.insertAll(push);
        return R.ok().put("data", count);
    }

    /**
     * 保存
     */
    @RequestMapping("/context")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R context(@RequestBody Map<String, Object> params) {
        return R.ok().put("data", configService.update(new ConfigEntity((String) params.get("context")), new LambdaQueryWrapper<ConfigEntity>().eq(ConfigEntity::getStatus, 0)));
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/update")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R update(@RequestBody PushEntity push, HttpSession session) {
        Object attribute = session.getAttribute(push.getDepartmentUid() + push.getYear());
        if (!Objects.isNull(attribute)) {
            String reportUid = (String) attribute;
            push.setReportUid(reportUid);
            pushService.update(push, new LambdaQueryWrapper<PushEntity>().eq(PushEntity::getDepartmentUid, push.getDepartmentUid()).eq(PushEntity::getYear, push.getYear()));
        }
        //修改task任务中的时间
        taskDao.updateTask(new TaskEntity(push.getStartTime(), push.getEndTime(), push.getUid()));
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R delete(@RequestBody String[] uids) {
        pushService.removeByIds(Arrays.asList(uids));

        return R.ok();
    }

}
